package com.xungou.goodtype.controller;

import com.xungou.goodtype.pojo.GoodType;
import com.xungou.goodtype.service.IGoodTypeService;
import com.xungou.user.pojo.User;
import com.xungou.user.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/goodType")
public class GoodTypeController {
    @Resource
    private IGoodTypeService goodTypeService;

    @RequestMapping("/add")
    @ResponseBody
    public Map<String, Object> add(GoodType goodType,@RequestParam Map<String, String> params) {
        int flag = goodTypeService.insert(goodType,params);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("flag",flag);
        result.put("id",goodType.getId());
        result.put("code",goodType.getCode());
        return result;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(GoodType goodType) {
        int flag = goodTypeService.delete(goodType.getId());
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("flag",flag);
        return result;
    }

    @RequestMapping("/page")
    public ModelAndView manage(){
        ModelAndView modelAndView = new ModelAndView();
        if(SecurityUtils.getSubject().isAuthenticated()&&SecurityUtils.getSubject().hasRole("ADMIN"))
            modelAndView.setViewName("manage/good/goodType");
        else{
            SecurityUtils.getSubject().logout();
            modelAndView.setViewName("global/login");
        }
        return modelAndView;
    }

    @RequestMapping("/getTypeTree")
    @ResponseBody
    public List<Map<String, Object>> getTypeTree() {
        List<Map<String, Object>> result = goodTypeService.getTypeTree();
        return result;
    }

    @RequestMapping("/getGoodTypeList")
    @ResponseBody
    public List<Map<String, Object>> getGoodTypeList(@RequestParam Map<String, String> params) {
        List<Map<String, Object>> result = goodTypeService.getGoodTypeList(params);
        return result;
    }

}